<template>
  <LoginHeader>联合登录</LoginHeader>
  <!-- 已经绑定了QQ账号 -->
  <section class="container" v-if="isBind">
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>
  <!-- 去绑定账号 -->
  <section class="container" v-else>
    <nav class="tab">
      <a @click="hasAccount = true" :class="{ active: hasAccount }" href="javascript:;">
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a @click="hasAccount = false" :class="{ active: !hasAccount }" href="javascript:;">
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <!-- 有账号的表单 -->
      <CallbackBind :unionId="unionId" />
    </div>
    <div class="tab-content" v-else>
      <!-- 没账号的表单，需要注册 -->
      <CallbackPatch :unionId="unionId" />
    </div>
  </section>
  <LoginFooter />
</template>
<script setup>
import LoginHeader from './login-header'
import LoginFooter from './login-footer'
import CallbackBind from './callback-bind'
import CallbackPatch from './callback-patch'
import { ref } from 'vue'
import QC from 'qc'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
import { userQQLogin } from '@/api/login'
import Message from '@/components/library/message'
const store = useStore()
const route = useRoute()
const router = useRouter()
// 假设用户已经绑定了QQ账号
const isBind = ref(true)
// 保存QQ的唯一标识，传递给子组件
const unionId = ref(null)
// 检查QQ是否已经授权登录了
if (QC.Login.check()) {
  // 调用QQ的Api去获取用户在QQ服务器的唯一标识openid
  QC.Login.getMe(async openid => {
    unionId.value = openid
    try {
      // 如果已经绑定了好,调接口会返回绑定的用户的信息
      const { result } = await userQQLogin(openid, 6)
      // 1. 存储用户信息
      const { id, account, avatar, mobile, nickname, token } = result
      store.commit('user/setUser', { id, account, avatar, mobile, nickname, token })
      // 合并购物车数据
      store.dispatch('cart/mergeCart').then(() => {
        // 2.提示信息
        Message({ type: 'success', text: 'QQ登录成功' })
        // 3.跳转
        router.push((route.query.redirectUrl && decodeURIComponent(route.query.redirectUrl)) || store.state.user.currentPath || '/')
      })
    } catch (err) {
      // 未绑定，代表：使用qq登录失败===>1. 没绑定小兔鲜帐号  2. 没有小兔鲜帐号
      isBind.value = false
    }
  })
}

// 1.有账号但是未和QQ绑定 2.没有账号，就先注册账号再和QQ绑定
// 通过hasAccount来判断是否有账号,默认有账号
const hasAccount = ref(true)
</script>
<style scoped lang='less'>
.container {
  padding: 25px 0;
  position: relative;
  height: 730px;

  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;

    .loading {
      height: 100%;
      background: #fff url(@/assets/images/load.gif) no-repeat center / 100px 100px;
    }
  }

  .tab {
    background: #fff;
    height: 80px;
    padding-top: 40px;
    font-size: 18px;
    text-align: center;

    a {
      color: #666;
      display: inline-block;
      width: 350px;
      line-height: 40px;
      border-bottom: 2px solid #e4e4e4;

      i {
        font-size: 22px;
        vertical-align: middle;
      }

      span {
        vertical-align: middle;
        margin-left: 4px;
      }

      &.active {
        color: @xtxColor;
        border-color: @xtxColor;
      }
    }
  }

  .tab-content {
    min-height: 600px;
    background: #fff;
  }
}
</style>